﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;


namespace WindowsFormsApplication1
{
    public partial class release_task : Form
    {
        public release_task()
        {
            InitializeComponent();
        }



        private void release_task_Load(object sender, EventArgs e)
        {
            load_combox();
            load_data();
        }
        private void load_combox()
        {
            string connection;
            string query;
            DataSet myds;
            connection = dbutils.constr;
            query = "select * from staff";
            MySqlConnection conn = new MySqlConnection(connection);
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            conn.Open();
            adapter.SelectCommand = new MySqlCommand(query, conn);
            myds = new DataSet();
            adapter.Fill(myds, "Bill");
            //bindingSource3.DataSource = myds.Tables["Bill"].Rows.;
            //comboBox1.DataSource = bindingSource3;
            comboBox1.DataSource = myds.Tables[0];
            comboBox1.DisplayMember = myds.Tables[0].Columns[0].ColumnName;//显示的文本
            comboBox1.ValueMember = myds.Tables[0].Columns[1].ColumnName;//对应的值
            conn.Close();
        }

        private void load_data()
        {
            string connection;
            string query;
            DataSet myds;
            connection = dbutils.constr;
            query = "select * from task order by release_date DESC";
            MySqlConnection conn = new MySqlConnection(connection);
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            conn.Open();
            adapter.SelectCommand = new MySqlCommand(query, conn);
            myds = new DataSet();
            adapter.Fill(myds, "Bill");
            bindingSource1.DataSource = myds.Tables["Bill"];
            dataGridView1.DataSource = bindingSource1;
            dataGridView1.Columns["num"].HeaderText = "任务编号";
            dataGridView1.Columns["staffid"].HeaderText = "员工编号";
            dataGridView1.Columns["staffname"].HeaderText = "员工姓名";
            dataGridView1.Columns["release_date"].HeaderText = "发布日期";
            dataGridView1.Columns["release_text"].HeaderText = "任务说明";
            dataGridView1.Columns["state"].HeaderText = "任务状态";
            conn.Close();



        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == null)
                return;
            if (textBox1.Text.Trim() == "")
                return;
            string staffid = comboBox1.SelectedValue.ToString();
            string staffname = comboBox1.GetItemText(comboBox1.Items[comboBox1.SelectedIndex]);
            if (MessageBox.Show("确定发布任务给 " + staffname + " 吗？", "提示", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                return;
            string date = DateTime.Now.ToString("yyyy-MM-dd");
            string text = textBox1.Text.Trim();
            string sql = "insert into task(staffid,staffname,release_date,release_text,state) values('"+staffid+"','"+staffname+"','"+date+"','"+text+"','已发布')";
            if (new dbutils().insert(sql) == 0)
            {
                MessageBox.Show("error!");
                return;
            }
            MessageBox.Show("操作成功！");
            load_data();
        }
    }
}
